package com.pc.controller;

import com.pc.service.FoodService;
import com.pc.service.RedisService;
import com.pc.utils.Result;
import com.pc.utils.aop.OperLog;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("food")
public class FoodController {
    @Resource
    private FoodService foodService;

    @Resource
    private RedisService  redisService;
    //查询菜品
    @GetMapping("/getCategoryFood/{category}")
    @OperLog(operModul = "菜品模块",operType = "查询",operDesc = "显示对应的菜品")
    public Result getCategoryFood(@PathVariable String category){
        return foodService.getCategoryFood(category);
    }
    //查询功能
    @GetMapping("/getCategory/{foodname}")
//    @OperLog(operModul = "菜品模块",operType = "查询",operDesc = "查询对应的菜品")
    public Result getCategory(@PathVariable String foodname){
        String category = foodService.getCategory(foodname);
        if(category.equals("没有查询到"))
            return Result.fail("本店没有这个菜品");
        else
            return Result.success(category);
    }
    //查询功能
    @GetMapping("/getCategoryRedis/{foodname}")
//    @OperLog(operModul = "菜品模块",operType = "查询",operDesc = "查询对应的菜品")
    public Result getCategoryRedis(@PathVariable String foodname){
        String key = redisService.getKey(foodname);
        if (key==null||key.isEmpty()){
            String category = foodService.getCategory(foodname);
            if(category.equals("没有查询到"))
                return Result.fail("本店没有这个菜品","1");
            else{
                redisService.setKey(foodname,category);
                return Result.success(category);
            }
        }
        else{
            return Result.success(redisService.getKey(foodname));
        }

    }
}
